package com.esms.controller;

import com.esms.dao.EmployeeCustomVoMapper;
import com.esms.dao.EmployeeMapper;
import com.esms.dao.MenuPermMapper;
import com.esms.dao.XWeekresultMapper;
import com.esms.po.*;
import com.esms.service.IResponsibilityService;
import com.esms.service.LoginMenuPermService;
import com.esms.service.MonthmentService;
import com.esms.service.impl.EmployeeWeekCheckServerImpl;
import com.esms.service.impl.EmployeeWeekServerImpl;
import com.esms.utils.DateUtil;
import com.esms.utils.Message;
import com.esms.utils.ScoreDeal;
import com.esms.utils.Text2Html;
import com.esms.utils.week.DateWeekUtil;
import com.esms.utils.week.MDA;
import com.esms.vo.*;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.util.StringUtil;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author : [ko]
 * @version : [v1.0]
 * @description : [周纪实审核处理Controller]
 * @createTime : [2022/3/5 0:14]
 * @updateUser : [ko]
 * @updateTime : [2022/3/5 0:14]
 * @updateRemark : [说明本次修改内容]
 */
@Controller
@RequestMapping("/employeeWeekCheck")
public class EmployeeWeekCheckController {

	@Autowired
	private EmployeeMapper employeeMapper = null;

	@Autowired
	private EmployeeCustomVoMapper employeeCustomVoMapper = null;

	@Autowired
	private EmployeeWeekServerImpl employeeWeekServerImpl;

	@Autowired
	private EmployeeWeekCheckServerImpl employeeWeekCheckServerImpl;

	@Autowired
	private LoginMenuPermService menuPermService;

	@Autowired
	private ScoreDeal scoreDeal;

	@Autowired
	private MenuPermMapper menuPermMapper;

	@Autowired
	IResponsibilityService responsibilityService;

	@Autowired
	XWeekresultMapper xWeekresultMapper;

	@RequestMapping("/findSelective.do")
	@ResponseBody
	public EmployeePages findSelective(
			@RequestParam(value = "page", defaultValue = "1") int pageNum,
			@RequestParam(value = "limit", defaultValue = "5") int limit,
			@RequestParam(value = "e_account", defaultValue = "") String e_account,
			@RequestParam(value = "e_name", defaultValue = "") String e_name,
			@RequestParam(value = "d_id", defaultValue = "0") int d_id,
			HttpSession httpSession) throws Exception {

		int eId = (Integer) httpSession.getAttribute("employeeId");
		Employee employee = employeeMapper.selectByPrimaryKey(eId);
		String cAccount = employee.geteAccount();// 当前登录员工工号,作为查询下属的入参
		//标题设置：当前日期上个月
		String befMouth = DateWeekUtil.getBefMonthForNowDay();
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("e_account", e_account);
		map.put("e_name", e_name);
		map.put("d_id", d_id);
		map.put("e_level", cAccount);
		//map.put("x_check_BeMonth", befMouth);//再加一个考核的月数
		List<EmployeeCustomVo> list = new ArrayList<EmployeeCustomVo>();
		try{
			//pageNum:起始页面  pageSize:每页的大小
			PageHelper.startPage(pageNum, limit);
			//查找条件，一定要紧跟在startPage后
			//第一步：先查被考核员工的信息
			list = employeeCustomVoMapper.selectEmployeeWeekCheckSelective(map);
			//第二步：再查这些员工，是否上个月已被审核打分了
			//需要根据查出的初始员工，再查询一下当前月上个月是否已经做了审核，如果做了审核，则给weekrment赋值，前端进行展示即可
			for(int k=0;k<list.size();k++) {
				if (list.get(k).getEmployee() != null) {
					int xStaffEId = list.get(k).getEmployee().geteId();
					if(xStaffEId != 0){
						//查询考核结果表，是否存在上个月的考核结果:入参就是被审核的工号xStaffEId+上个月月份befMouth
						WeekResultInfoMent weekResultInfoMent = new WeekResultInfoMent();
						weekResultInfoMent.setxStaffEId(xStaffEId);//被审核员工的ID
						weekResultInfoMent.setxSuperEId(eId);//审核员工的ID
						weekResultInfoMent.setxCheckBeMonth(befMouth);
						WeekResultInfoMent weekrment = employeeWeekCheckServerImpl.findWeekResultInfo(weekResultInfoMent);
						if(weekrment != null){
							weekrment.setxCheckState(weekrment.getxCheckState());
							weekrment.setxCheckScore(weekrment.getxCheckScore());
							list.get(k).setWeekrment(weekrment);
						}
					}
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		PageInfo pageResult = new PageInfo(list);
		//设置前台需要的数据
		EmployeePages employeePages = new EmployeePages();
		employeePages.setCode(0);
		employeePages.setMsg("");
		employeePages.setCount((int) pageResult.getTotal());
		employeePages.setData(pageResult.getList());
		return employeePages;
	}


	/**
	 * 查找被审核员工填写的周纪实信息,用于前端的展示，最终需要审核人员进行审核打分：
	 * 因为审核人员只有每个月初对上个月的员工周纪实进行评价，
	 * 所以此处一定要获取员工上个月的所有周纪实内容进行展示即可
	 * @param byEId   被考核员工的ID
	 * @param byDId   被考核员工的部门ID
	 * @param byEAccount   被考核员工的工号
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findEmpWeekInfoSelective.do")
	@ResponseBody
	public EmployeeWeekGrandVO findDempSelective(
			@RequestParam(value="page", defaultValue="1")int pageNum,
			@RequestParam(value="limit", defaultValue="5") int limit,
			@RequestParam String byEId,
			@RequestParam String byDId,
			@RequestParam String byEAccount,
			@RequestParam String chooseMonth,
			HttpSession httpSession) throws Exception {

		WeekRecordInfoMent wWeekMent = new WeekRecordInfoMent();
		//标题设置：当前日期上个月
		String befMouth = DateWeekUtil.getBefMonthForNowDay();

		wWeekMent.setwAccount(byEAccount);//被评价员工工号
		wWeekMent.setwDId(Integer.valueOf(byDId));//被评价员工部门
		if(StringUtil.isNotEmpty(chooseMonth)){
			if(MDA.WEEK_MONTH.contains(chooseMonth)){
				chooseMonth = "0"+chooseMonth;
			}
			wWeekMent.setwWeekMonYear(chooseMonth);//该员工第几个月的周纪实，获取当前日期上个月的周纪实
		}else{//默认初始进来是上个月的周纪实
			wWeekMent.setwWeekMonYear(befMouth);//该员工第几个月的周纪实，获取当前日期上个月的周纪实
		}
		//wWeekMent.setwWeekMonYear(befMouth);//该员工第几个月的周纪实，获取当前日期上个月的周纪实
		//wWeekMent.setwFlag(wFlag);
		//wWeekMent.setwFinshLivel(wFinshLivel);

		List<WeekRecordInfoMent> weekInfoListNew = new ArrayList<WeekRecordInfoMent>();
		try {
			List<WeekRecordInfoMent> weekInfoList = employeeWeekServerImpl.getEmpDeployeeWeekList(wWeekMent);
			if(weekInfoList.size()>0){
				for(WeekRecordInfoMent weekRecordInfoMent:weekInfoList){
					String weekInfo = weekRecordInfoMent.getwWeekInfo();
					String zdgzInfo = weekRecordInfoMent.getwZdgzInfo();
					String cxgzInfo = weekRecordInfoMent.getwCxgzInfo();
					String wTempOraryInfo = weekRecordInfoMent.getwTempOraryInfo();
					if(StringUtil.isNotEmpty(weekInfo)){//日常工作
						weekRecordInfoMent.setwWeekInfo(Text2Html.TextToHtml(weekInfo));
					}
					if(StringUtil.isNotEmpty(zdgzInfo)){//重点工作
						weekRecordInfoMent.setwZdgzInfo(Text2Html.TextToHtml(zdgzInfo));
					}
					if(StringUtil.isNotEmpty(cxgzInfo)){//创新工作
						weekRecordInfoMent.setwCxgzInfo(Text2Html.TextToHtml(cxgzInfo));
					}
					if(StringUtil.isNotEmpty(wTempOraryInfo)){//其他工作
						weekRecordInfoMent.setwTempOraryInfo(Text2Html.TextToHtml(wTempOraryInfo));
					}
					weekInfoListNew.add(weekRecordInfoMent);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		//设置前台需要的数据
		EmployeeWeekGrandVO employeeWeekGrandVO = new EmployeeWeekGrandVO();
		employeeWeekGrandVO.setCode(0);
		employeeWeekGrandVO.setMsg("");
		employeeWeekGrandVO.setCount(weekInfoListNew.size());
		employeeWeekGrandVO.setData(weekInfoListNew);
		return employeeWeekGrandVO;
	}


	/**
	 * 打印员工审核成绩查询接口主要查询被审核员工的分数、月数、月起始和截止日期
	 * @param byEId   被考核员工的ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findWeekResultInfoSelective.do")
	@ResponseBody
	public EmployeeWeekResultPages findWeekResultInfoSelective(
			@RequestParam(value="page", defaultValue="1")int pageNum,
			@RequestParam(value="limit", defaultValue="5") int limit,
			@RequestParam String byEId,
			HttpSession httpSession) throws Exception {

		int eId = (Integer) httpSession.getAttribute("employeeId");//当前登陆者的ID

		//标题设置：当前日期上个月
		String befMouth = DateWeekUtil.getBefMonthForNowDay();

		WeekResultInfoMent weekResultInfoMent = new WeekResultInfoMent();
		weekResultInfoMent.setxStaffEId(Integer.valueOf(byEId));//被审核员工的ID
		weekResultInfoMent.setxSuperEId(eId);//审核员工的ID
		weekResultInfoMent.setxCheckBeMonth(befMouth);

		List<WeekResultInfoMent> list = new ArrayList<WeekResultInfoMent>();
		try{
			//pageNum:起始页面  pageSize:每页的大小
			PageHelper.startPage(pageNum, limit);
			//查找条件，一定要紧跟在startPage后

			list = employeeWeekCheckServerImpl.findWeekResultInfoSelective(weekResultInfoMent);

		}catch (Exception e){
			e.printStackTrace();
		}
		PageInfo pageResult = new PageInfo(list);
		//设置前台需要的数据
		EmployeeWeekResultPages employeeWeekResultPages = new EmployeeWeekResultPages();
		employeeWeekResultPages.setCode(0);
		employeeWeekResultPages.setMsg("");
		employeeWeekResultPages.setCount((int) pageResult.getTotal());
		employeeWeekResultPages.setData(pageResult.getList());
		return employeeWeekResultPages;
	}

	@RequestMapping("/addWeekCheckInfo.do")
	@ResponseBody
	public Message addWeekCheckInfo(
			@RequestParam("list") String listWeekCheck,
			@RequestParam String byEId,
			@RequestParam String chooseMonth,
			HttpSession httpSession) throws Exception {
		int req = 0;
		ObjectMapper mapper = new ObjectMapper();
		Timestamp date = new Timestamp(new Date().getTime());
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");

		//01-当前登录员工信息
		Integer eId = (Integer) httpSession.getAttribute("employeeId");
		//02-被审核者的员工信息
		Employee byEmployee = employeeMapper.selectByPrimaryKey(Integer.valueOf(byEId));

		try {
			JavaType weekJt = mapper.getTypeFactory().constructParametricType(ArrayList.class, WeekCheckGrand.class);
			List<WeekCheckGrand> listWeekCheckGrand = (List<WeekCheckGrand>) mapper.readValue(listWeekCheck, weekJt);

			for (int i = 0; i < listWeekCheckGrand.size(); i++) {
				WeekCheckGrand weekCheckGrand = listWeekCheckGrand.get(i);

				//1-下面两个是领导审核的日期，领导月初审核上个月一整月的周纪实,计算当前日期属于第几季度:领导审核的日期
				String xCreateQuarter = DateWeekUtil.getQuarterByDate(currentTime);
				/**2-计算当前日期属于哪年哪个月的第几周,例如：2022-2-5  2022年2月第5周
				int intDate = Integer.valueOf(formatter.format(currentTime));
				DateBean dateBean = DateWeekUtil.getWeekByDay(intDate);
				String year = dateBean.getYear();//归属年
				String month = dateBean.getMonth();//归属月：标题设置：第几月  报表需要
				String week = dateBean.getWeek();//归属周：标题设置：本月第几周 报表需要
				String xCreateWeek = year+month+week;
				 **/

				//打分的周数归属的月份需要记录，以及打分所在月数的时间
				//标题设置：当前日期上个月
				//String beMonthDay = DateUtil.getBeforeMonDay();//上个月开始日期和结束日期

				if(StringUtil.isNotEmpty(chooseMonth)){
					if(MDA.WEEK_MONTH.contains(chooseMonth)){
						chooseMonth = "0"+chooseMonth;
					}
				}

				WeekResultInfoMent weekResultInfoMent = new WeekResultInfoMent();
				weekResultInfoMent.setxStaffEId(Integer.valueOf(byEId));//被审核员工的ID
				weekResultInfoMent.setxSuperEId(eId);//审核员工的ID
				weekResultInfoMent.setxCheckBeMonth(chooseMonth);
				List<WeekResultInfoMent> empWeekResultList = employeeWeekCheckServerImpl.getEmpWeekResultList(weekResultInfoMent);
				if(empWeekResultList.size()>0){
					//直接修改即可

					//String xCheckState = weekCheckGrand.getxCheckState();//被审核员工状态
					String xCheckScore = weekCheckGrand.getxCheckScore();//被审核员工分数
					String xCheckResult = weekCheckGrand.getxCheckResult();//被审核员工审核结果描述

					String xCheckScorecalu = scoreDeal.scoreCalculationWeekResult(Integer.valueOf(xCheckScore));

					weekResultInfoMent.setxCheckScore(Integer.valueOf(xCheckScore));
					weekResultInfoMent.setxCheckScoresum(xCheckScorecalu);
					weekResultInfoMent.setxCheckScorecalu(xCheckScorecalu);
					weekResultInfoMent.setxCreateQuarter(xCreateQuarter);
					weekResultInfoMent.setxCreateWeek("");
					weekResultInfoMent.setxCheckBeMonth(chooseMonth);
					weekResultInfoMent.setxCheckBeMonthDay("");
					weekResultInfoMent.setxCheckResult(xCheckResult);
					weekResultInfoMent.setxCheckState("1");
					weekResultInfoMent.setxUpdateTime(date);

					//直接修改即可
					req = employeeWeekCheckServerImpl.updateByPrimaryKeySelective(weekResultInfoMent);

				}else{

					String xStaffEId = weekCheckGrand.getxStaffEId();//被审核员工ID
					//String xCheckState = weekCheckGrand.getxCheckState();//被审核员工状态
					String xCheckScore = weekCheckGrand.getxCheckScore();//被审核员工分数
					String xCheckResult = weekCheckGrand.getxCheckResult();//被审核员工审核结果描述
					//先判断是否已经存在了，根据评价者的ID和被评价者的ID查询即可

					String xCheckScorecalu = scoreDeal.scoreCalculationWeekResult(Integer.valueOf(xCheckScore));


					weekResultInfoMent.setxId(null);
					weekResultInfoMent.setxStaffEId(Integer.valueOf(xStaffEId));
					weekResultInfoMent.setxSuperEId(eId);
					weekResultInfoMent.setxCheckState("1");
					weekResultInfoMent.setxCheckResult(xCheckResult);
					weekResultInfoMent.setxCreateTime(date);
					weekResultInfoMent.setxUpdateTime(date);
					weekResultInfoMent.setxCreateQuarter(xCreateQuarter);
					weekResultInfoMent.setxCreateWeek("");
					weekResultInfoMent.setxCheckBeMonth(chooseMonth);
					weekResultInfoMent.setxCheckBeMonthDay("");
					weekResultInfoMent.setxCheckScore(Integer.valueOf(xCheckScore));
					weekResultInfoMent.setxCheckScoresum(xCheckScorecalu);
					weekResultInfoMent.setxCheckScorecalu(xCheckScorecalu);
					weekResultInfoMent.setxByzd("");
					weekResultInfoMent.setxByzd1("");
					weekResultInfoMent.setxByzd2("");
					// req = employeeWeekServerImpl.deleteWeekInfo(wWeekMent);
					req = employeeWeekCheckServerImpl.insertWeekCheckInfo(weekResultInfoMent);
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		if (req < 0) {
			return Message.fail();
		} else {
			//审核成功，需要修改一下呗审核员工周纪实数据的审核状态，因为初始入库的是0未审核，需要更新成1已审核
			// 重新设置修改时间
			WeekRecordInfoMent weekRecordInfoMent = new WeekRecordInfoMent();
			weekRecordInfoMent.setwAccount(byEmployee.geteAccount());
			weekRecordInfoMent.setwWeekMonYear(chooseMonth);//加上月份
			weekRecordInfoMent.setwUpdateTime(date);
			weekRecordInfoMent.setwCheckStatus("1");
			employeeWeekServerImpl.updateByWaccountSelective(weekRecordInfoMent);


			return Message.success("周记实审核提交成功");
		}
	}


	/**
	 * 查找被审核员工填写的周纪实信息,用于前端的展示，最终需要审核人员进行审核打分
	 * @param byEId   被考核员工的ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findEmpWeekResultInfoSelective.do")
	@ResponseBody
	public EmployeeWeekCheckGrandVO findEmpWeekResultInfoSelective(
			@RequestParam(value="page", defaultValue="1")int pageNum,
			@RequestParam(value="limit", defaultValue="5") int limit,
			@RequestParam String byEId,
			@RequestParam String chooseMonth,
			HttpSession httpSession) throws Exception {

		//当前登陆者的ID,就是审核者的ID
		int eId = (Integer) httpSession.getAttribute("employeeId");

		//标题设置：当前日期上个月
		String befMouth = DateWeekUtil.getBefMonthForNowDay();

		WeekResultInfoMent weekResultInfo = new WeekResultInfoMent();

		weekResultInfo.setxStaffEId(Integer.valueOf(byEId));//被审核员工的ID
		weekResultInfo.setxSuperEId(eId);//审核员工的ID

		if(StringUtil.isNotEmpty(chooseMonth)){
			if(MDA.WEEK_MONTH.contains(chooseMonth)){
				chooseMonth = "0"+chooseMonth;
			}
			weekResultInfo.setxCheckBeMonth(chooseMonth);
		}else{
			weekResultInfo.setxCheckBeMonth(befMouth);
		}

		List<WeekResultInfoMent> weekCheckInfoListNew = new ArrayList<WeekResultInfoMent>();
		try {
			List<WeekResultInfoMent> weekInfoList = employeeWeekCheckServerImpl.getEmpWeekResultList(weekResultInfo);
			if(weekInfoList.size()>0){
				for(WeekResultInfoMent weekResultInfoMent:weekInfoList){
					String weekcheckInfo = weekResultInfoMent.getxCheckResult();
					weekCheckInfoListNew.add(weekResultInfoMent);
					/**
					if(StringUtil.isNotEmpty(weekcheckInfo)){
						weekResultInfoMent.setxCheckResult(Text2Html(weekcheckInfo));
						weekCheckInfoListNew.add(weekResultInfoMent);
					}
					 **/
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		//设置前台需要的数据
		EmployeeWeekCheckGrandVO employeeWeekCheckGrandVO = new EmployeeWeekCheckGrandVO();
		employeeWeekCheckGrandVO.setCode(0);
		employeeWeekCheckGrandVO.setMsg("");
		employeeWeekCheckGrandVO.setCount(weekCheckInfoListNew.size());
		employeeWeekCheckGrandVO.setData(weekCheckInfoListNew);
		return employeeWeekCheckGrandVO;
	}

	@Autowired
	public MonthmentService monthmentService;
	/**
	 * 查找月份数
	 *
	 * @param pageNum
	 * @param limit
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findMonthSelective.do")
	@ResponseBody
	public MonthPages findMonthSelective(
			@RequestParam(value = "page", defaultValue = "1") int pageNum,
			@RequestParam(value = "limit", defaultValue = "15") int limit,
			@RequestParam(value = "name", defaultValue = "") String name) throws Exception {
		List<Monthment> list = new ArrayList<Monthment>();
		//模糊查询，有多少个条件就接收多少个字段
		Monthment monthment = new Monthment();
		monthment.setmName(name);
		//pageNum:起始页面  pageSize:每页的大小
		PageHelper.startPage(pageNum, limit);
		try{
			//查找条件，一定要紧跟在startPage后
			list = monthmentService.findMonthSelective(monthment);
		}catch (Exception e){
			e.printStackTrace();
		}
		PageInfo pageResult = new PageInfo(list);
		//设置前台需要的数据
		MonthPages monthPages = new MonthPages();
		monthPages.setCode(0);
		monthPages.setMsg("");
		monthPages.setCount((int) pageResult.getTotal());
		monthPages.setData(pageResult.getList());
		return monthPages;
	}

	/**
	 * 员工岗位职责详情展示
	 * @param byEAccount   被评员工的工号
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findEmpJobInfo.do")
	@ResponseBody
	public EmployeeResponDetailVO findEmpJobInfo(
			@RequestParam(value="page", defaultValue="1")int pageNum,
			@RequestParam(value="limit", defaultValue="5") int limit,
			@RequestParam String byEAccount,
			HttpSession httpSession) throws Exception {


		//map里面的入参，要和mapper.xml文件中的条件一致
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("r_type","1");
		map.put("r_byzd",byEAccount);

		List<Responsibility> responsibilityList = new ArrayList<Responsibility>();
		List<Responsibility> responsibilityListNew = new ArrayList<Responsibility>();

		try{
			responsibilityList = responsibilityService.findEmpJobInfo(map);
			if(responsibilityList.size()>0){
				for(Responsibility responsibility:responsibilityList){
					String content = responsibility.getrContent();
					if(StringUtil.isNotEmpty(content)){//岗位职责
						responsibility.setrContent(Text2Html.TextToHtml(content));
					}
					responsibilityList.clear();
					responsibilityList.add(responsibility);
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}

		//设置前台需要的数据
		EmployeeResponDetailVO employeeResponDetailVO = new EmployeeResponDetailVO();
		employeeResponDetailVO.setCode(0);
		employeeResponDetailVO.setMsg("");
		employeeResponDetailVO.setCount(responsibilityList.size());
		employeeResponDetailVO.setData(responsibilityList);
		return employeeResponDetailVO;
	}


	/**
	 * 周记实打分情况
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/scoresStatistical.do")
	@ResponseBody
	public WeekResScoringPages scoresStatistical(
			@RequestParam(value="page", defaultValue="1")int pageNum,
			@RequestParam(value="limit", defaultValue="5") int limit,
			@RequestParam(value = "year",defaultValue = "0",required = false) int year,
			@RequestParam(value = "month",defaultValue = "0",required = false) String month,
			HttpSession httpSession) throws Exception {
		WeekResScoringPages weekResScoringPages = new WeekResScoringPages();
		// 检查权限
		int eId = (Integer) httpSession.getAttribute("employeeId");
		String account = employeeMapper.selectByPrimaryKey(eId).geteAccount();

		if (!menuPermService.whetherHasMenu("3",account)) {
			weekResScoringPages.setCode(100);
			weekResScoringPages.setMsg("无权限");
			return weekResScoringPages;
		}

		if (month.equals("0")){
			month = DateUtil.getBefMonthForNowDay();

			if (month.equals("00")){
				month="12";
			}
		}
		if (month.length()==1){
			month = "0"+month;
		}
		if (year==0){
			year = cn.hutool.core.date.DateUtil.thisYear();
		}
		if (month.equals("12")){
			year+=1;
		}
		List<WeekResScoringStatistic> list = new ArrayList<WeekResScoringStatistic>();
		PageHelper.startPage(pageNum, limit);
		try{
			//查找条件，一定要紧跟在startPage后
			 list = xWeekresultMapper.listWeekResScoringStatistic(month,String.valueOf(year));
		}catch (Exception e){
			e.printStackTrace();
		}
		PageInfo pageResult = new PageInfo(list);
		//设置前台需要的数据
		weekResScoringPages.setCode(0);
		weekResScoringPages.setMsg("");
		weekResScoringPages.setCount((int) pageResult.getTotal());
		weekResScoringPages.setData(pageResult.getList());


		return weekResScoringPages;
	}

	@RequestMapping("/scoresStatisticalExcel.do")
	public Message scoresStatisticalExcel(
			@RequestParam(value = "year",defaultValue = "0",required = false) int year,
			@RequestParam(value = "month",defaultValue = "0",required = false) String month,
			HttpSession httpSession, HttpServletResponse response) throws IOException {
		WeekResScoringPages weekResScoringPages = new WeekResScoringPages();
		// 检查权限
////		int eId = (Integer) httpSession.getAttribute("employeeId");
//		String account = employeeMapper.selectByPrimaryKey(eId).geteAccount();
//		MenuPerm menuPerm = menuPermMapper.selectShowMenuPermAccountByFlag("3");
//		if (!menuPerm.getMenuAccount().contains(account)){
//			return Message.error("无权限");
//		}

		if (year==0 && !month.equals("0")){
			return Message.error("不可以年为0月不为0");
		}

		if (!month.equals("0") && month.length()==1){
			month = "0"+month;
		}
		List<WeekResScoringStatisticWithTime> statisticWithTimes = new ArrayList<>();
		statisticWithTimes = xWeekresultMapper.listWeekResScoringByTime(month,year);

		//创建excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		//创建sheet页
		if (month.equals("0")){
			month="全部";
		}
		HSSFSheet sheet = wb.createSheet("周纪实分数统计"+year+"年"+month+"月");
		//创建标题行
		HSSFRow titleRow = sheet.createRow(0);
		titleRow.createCell(0).setCellValue("工号");
		titleRow.createCell(1).setCellValue("姓名");
		titleRow.createCell(2).setCellValue("部门");
		titleRow.createCell(3).setCellValue("分数");
		titleRow.createCell(4).setCellValue("部门类型");
		titleRow.createCell(5).setCellValue("年份");
		titleRow.createCell(6).setCellValue("月份");

		//遍历将数据放到excel列中
		for (WeekResScoringStatisticWithTime item:
				statisticWithTimes) {
			HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			dataRow.createCell(0).setCellValue(item.geteAccount());
			dataRow.createCell(1).setCellValue(item.geteName());
			dataRow.createCell(2).setCellValue(item.getdName());
			dataRow.createCell(3).setCellValue(item.getxCheckScore());
			dataRow.createCell(4).setCellValue(item.getCategory());
			dataRow.createCell(5).setCellValue(item.getYear());
			dataRow.createCell(6).setCellValue(item.getMonth());
		}


		// 设置下载时客户端Excel的名称   （上面注释的改进版本，上面的中文不支持）
		response.setContentType("application/octet-stream;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename="
				+ new String("周纪实分数统计".getBytes(), "iso-8859-1") + ".xls");


		OutputStream ouputStream = response.getOutputStream();
		wb.write(ouputStream);
		ouputStream.flush();
		ouputStream.close();


		return Message.success();

	}

	@RequestMapping("/scoresNoStatistical.do")
	@ResponseBody
	public WeekResNoScoringPages scoresNoStatistical(
			@RequestParam(value="page", defaultValue="1")int pageNum,
			@RequestParam(value="limit", defaultValue="5") int limit,
			@RequestParam(value = "year",defaultValue = "0",required = false) int year,
			@RequestParam(value = "month",defaultValue = "0",required = false) String month,
			HttpSession httpSession) throws Exception {
		WeekResNoScoringPages weekResNoScoringPages = new WeekResNoScoringPages();
		// 检查权限
		int eId = (Integer) httpSession.getAttribute("employeeId");
		String account = employeeMapper.selectByPrimaryKey(eId).geteAccount();


		if (!menuPermService.whetherHasMenu("2",account)) {
			weekResNoScoringPages.setCode(100);
			weekResNoScoringPages.setMsg("无权限");
			return weekResNoScoringPages;
		}

		if (month.equals("0")){
			Calendar calendar = Calendar.getInstance();
			//记得要+1
			month = Integer.toString(calendar.get(Calendar.MONTH) + 1);
//			if (month.equals("00")){
//				month="12";
//			}
		}
		if (month.length()==1){
			month = "0"+month;
		}
		if (year==0){
			year = cn.hutool.core.date.DateUtil.thisYear();
		}
//		if (month.equals("12")){
//			year+=1;
//		}
		List<WeekResNoScoringStatistic> list = new ArrayList<WeekResNoScoringStatistic>();
		PageHelper.startPage(pageNum, limit);
		try{
			//查找条件，一定要紧跟在startPage后
			list = xWeekresultMapper.listWeekNoResScoringStatistic(month,String.valueOf(year));
		}catch (Exception e){
			e.printStackTrace();
		}
		PageInfo pageResult = new PageInfo(list);
		//设置前台需要的数据
		weekResNoScoringPages.setCode(0);
		weekResNoScoringPages.setMsg("");
		weekResNoScoringPages.setCount((int) pageResult.getTotal());
		weekResNoScoringPages.setData(pageResult.getList());

		return weekResNoScoringPages;
	}

	@RequestMapping("/scoresNoStatisticalExcel.do")
	public Message scoresNoStatisticalExcel(
			@RequestParam(value = "year",defaultValue = "0",required = false) int year,
			@RequestParam(value = "month",defaultValue = "0",required = false) String month,
			HttpSession httpSession, HttpServletResponse response) throws IOException {
		WeekResScoringPages weekResScoringPages = new WeekResScoringPages();
		// 检查权限
////		int eId = (Integer) httpSession.getAttribute("employeeId");
//		String account = employeeMapper.selectByPrimaryKey(eId).geteAccount();
//		MenuPerm menuPerm = menuPermMapper.selectShowMenuPermAccountByFlag("3");
//		if (!menuPerm.getMenuAccount().contains(account)){
//			return Message.error("无权限");
//		}

		if (year==0 && !month.equals("0")){
			return Message.error("不可以年为0月不为0");
		}

		if (!month.equals("0") && month.length()==1){
			month = "0"+month;
		}
		List<WeekResNoScoringStatisticWithTime> noStatisticWithTimes = new ArrayList<>();
		noStatisticWithTimes = xWeekresultMapper.listWeekResNoScoringByTime(month,year);


		//创建excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		//创建sheet页
		if (month.equals("0")){
			month="全部";
		}
		HSSFSheet sheet = wb.createSheet("周纪实未打分统计"+year+"年"+month+"月");
		//创建标题行
		HSSFRow titleRow = sheet.createRow(0);
		titleRow.createCell(0).setCellValue("工号");
		titleRow.createCell(1).setCellValue("姓名");
		titleRow.createCell(2).setCellValue("评价人工号");
		titleRow.createCell(3).setCellValue("评价人姓名");
		titleRow.createCell(4).setCellValue("编制类型");
		titleRow.createCell(5).setCellValue("年份");
		titleRow.createCell(6).setCellValue("月份");


		//遍历将数据放到excel列中
		for (WeekResNoScoringStatisticWithTime item: noStatisticWithTimes) {
			HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			dataRow.createCell(0).setCellValue(item.geteAccount());
			dataRow.createCell(1).setCellValue(item.geteName());
			dataRow.createCell(2).setCellValue(item.geteLevel());
			dataRow.createCell(3).setCellValue(item.getBeName());
			dataRow.createCell(4).setCellValue(item.getCategory());
			dataRow.createCell(5).setCellValue(year);
			dataRow.createCell(6).setCellValue(month);

		}


		// 设置下载时客户端Excel的名称   （上面注释的改进版本，上面的中文不支持）
		response.setContentType("application/octet-stream;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename="
				+ new String("周纪实未打分统计".getBytes(), "iso-8859-1") + ".xls");


		OutputStream ouputStream = response.getOutputStream();
		wb.write(ouputStream);
		ouputStream.flush();
		ouputStream.close();


		return Message.success();

	}


}
